package com.ruoyix.weblog.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.core.validate.QueryGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.utils.poi.ExcelUtil;
import com.ruoyix.weblog.domain.vo.BlogWeiYanVo;
import com.ruoyix.weblog.domain.bo.BlogWeiYanBo;
import com.ruoyix.weblog.service.IBlogWeiYanService;
import com.ruoyix.common.core.page.TableDataInfo;

/**
 * 微言
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/weblog/weiYan")
public class BlogWeiYanController extends BaseController {

    private final IBlogWeiYanService iBlogWeiYanService;

    /**
     * 查询微言列表
     */
    @SaCheckPermission("weblog:weiYan:list")
    @GetMapping("/list")
    @SaIgnore
    public TableDataInfo<BlogWeiYanVo> list(BlogWeiYanBo bo, PageQuery pageQuery) {
        return iBlogWeiYanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出微言列表
     */
    @SaCheckPermission("weblog:weiYan:export")
    @Log(title = "微言", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BlogWeiYanBo bo, HttpServletResponse response) {
        List<BlogWeiYanVo> list = iBlogWeiYanService.queryList(bo);
        ExcelUtil.exportExcel(list, "微言", BlogWeiYanVo.class, response);
    }

    /**
     * 获取微言详细信息
     *
     * @param id 主键
     */
    @SaIgnore
//    @SaCheckPermission("weblog:weiYan:query")
    @GetMapping("/{id}")
    public R<BlogWeiYanVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBlogWeiYanService.queryById(id));
    }

    /**
     * 新增微言
     */
    @SaCheckPermission("weblog:weiYan:add")
    @Log(title = "微言", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogWeiYanBo bo) {
        return toAjax(iBlogWeiYanService.insertByBo(bo));
    }

    /**
     * 修改微言
     */
    @SaCheckPermission("weblog:weiYan:edit")
    @Log(title = "微言", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogWeiYanBo bo) {
        return toAjax(iBlogWeiYanService.updateByBo(bo));
    }

    /**
     * 删除微言
     *
     * @param ids 主键串
     */
    @SaCheckPermission("weblog:weiYan:remove")
    @Log(title = "微言", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBlogWeiYanService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
